﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using CQIE.TES.IService;
using CQIE.TES.Model;
using CQIE.TES.WEB.ADMIN.Helper;

namespace CQIE.TES.WEB.ADMIN.Controllers
{
    public class LoginController : Controller
    {
        public IAdminService AdminService
        {
            get;
            set;
        }
        // GET: Login
        public ActionResult Index()
        {
            Session["AID"] = null;
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string account, string pwd,string vicode)
        {

            if (vicode!=TempData["ViCode"].ToString())
            {
                ViewBag.error = "验证码错误！";
                return View("Index");
            }
            Admin User = AdminService.Login(account, pwd);
            if (User!=null)
            {
                Session["AID"] = User;
                return Redirect("/Home");
            }

            ViewBag.error = "用户名或密码错误！";
            return View("Index");
        }

        public ActionResult ViCode()
        {
           string code = VerificationHelper.CreateRandomCode(4);
           TempData["ViCode"] = code;
           return File(VerificationHelper.CreateValidGraphic(code), "image/jpeg");
        }
    }
}